import MapFeature from "./MapFeature";
import * as events from '../event';
import * as mapUtil from '../util';
import * as ftypes from '../featureTypes'
import { message } from "ant-design-vue";
import * as tooltip from '../tooltip';
import * as api from '../../../util/api';
import Feature from 'ol/Feature';
import { Polygon } from 'ol/geom';
import Base from './base'
import { mapManager } from "../MapManager";
import PartitionStyle from "../style/partitionStyle";

/**
 * 分区信息
 */
class Partition extends MapFeature {

    

    /**
   * 构造函数
   * @param {Feature} f 
   * @param {import("../../../util/typedef").PartitionInfo} valueObject - 分区信息
   */
    constructor(f, valueObject) {
        super(f);
        if (f == null) throw 'error';

        //初始化属性
        if (f) {
            this.setArea(mapUtil.formatArea(f.getGeometry()));
        }

        if (valueObject) {
            this.setValueOjbect(valueObject);
        } else {
            if (f.get("_valueobject") == undefined) {
                this.setValueOjbect({
                    viewColor:PartitionStyle.defaultColor
                });
            } 
        }

        this.setType(ftypes.PARTITIONS); 
    }

    /**
     * 开始标记分区
     */
    static beginDraw() {

        tooltip.showTip();

        MapFeature.innerDraw({ ftype: ftypes.PARTITIONS, style: PartitionStyle.drawPartitionStyle() }, (evt) => {
            evt.feature.setStyle(PartitionStyle.drawPartitionStyle());
            evt.feature.set("ftype", ftypes.PARTITIONS);
            var base = Partition.partitionInBase(evt.feature);
            if (base == null) {
                message.error("分区需要规划在基地中!");
                return false;
            }
            var baseWrapper = new Base(base);
            var baseInfo = baseWrapper.getValueObject();
            var partitionFeature = new Partition(evt.feature, {
                baseInfo: baseInfo,
                opacity: 0.1,
                viewColor: '#ffffff'
            });
            
            events.fire(events.types.onAddPartition, {
                type: 'create',
                data: partitionFeature
            });
        })
    }

    /**
    * 判断分区是否完整的在某个基地范围内
    */
    static partitionInBase(feature) {
        var returnFeature;
        var features = mapManager.current.layers.baseSource.getFeatures();
        var coordinates = feature.getGeometry().getCoordinates();
        var latlng = coordinates[0][0];
        for (var x = 0, y = features.length; x < y; x++) {
            var feature_temp = features[x];
            var isIn = feature_temp.getGeometry().intersectsCoordinate(latlng);
            if (isIn) {
                returnFeature = feature_temp;
                break;
            } else if (x == y - 1) {
                return null;
            }
        }
        for (var i = 1, j = coordinates[0].length; i < j; i++) {
            latlng = coordinates[0][i];
            isIn = returnFeature.getGeometry().intersectsCoordinate(latlng);
            if (!isIn) {
                return null;
            }
        }
        return returnFeature;
    }


    /**
     * 进入编辑模式
     */
    doEdit() {

        //通知事件
        events.fire(events.types.onAddPartition, { type: 'update', data: this });

        //调用父类方法，编辑
        MapFeature.editFeature(this.feature, (evt) => {
            var feature_temp = evt.features.getArray()[0];
            var baseId = this.getValueObject().baseInfo.id;
            var base = mapManager.current.layers.baseSource.getFeatureById(Base.getFeatureIdByBaseId(baseId));
            var isIn = base.getGeometry().intersectsCoordinate(evt.mapBrowserEvent.coordinate);
            if (!isIn) {
                message.error('分区必须在基地范围内');
                return false;
            }

            var existPartition = mapUtil.tunnelInfoInPolygon(feature_temp, mapManager.current.layers.partitionsSource, true);
            if (existPartition != null) {
                var existId = existPartition.getId();
                var currentId = feature_temp.getId();
                console.log('existId='+existId)
                console.log(currentId)
                if (currentId != existId) {
                    message.error("当前分区与已经存在的分区有重叠!");
                    return false;
                }
            }

            //通知更新 
            events.fire(events.types.onAddPartition, {
                type: 'update',
                data: this
            });

            return true;
        });

    }




    /**
     * 初始化数据
     */
    static initData() {
        api.get(api.ZYGL_PARTITION_LIST, (resp) => {
            var partitions = resp.data;
            //加载分区数据
            for (var i = 0, j = partitions.length; i < j; i++) {
                var partition = partitions[i];
                var coordinateGroup = mapUtil.getCoordinateGroup(partition.coordinateGroup);
                if (coordinateGroup == null) continue;

                var polygon = new Feature({
                    ftype: ftypes.PARTITIONS,
                    geometry: new Polygon(coordinateGroup.coordinates),
                    name: partition.name
                });
                
                var partitionFeature = new Partition(polygon, partition);
                partitionFeature.setId(partition.id);
                //设置样式
                var style = PartitionStyle.getStyle(partition);
                partitionFeature.setStyle(style);

                mapManager.current.layers.partitionsSource.addFeature(partitionFeature.getFeature());
                
            }

        });
    }

    setParitionId(id) {
        this.feature.set("partition_id", id);
    }

    getPartitionId() {
        return this.feature.get("partition_id");
    }


    /**
    *  ajax 保存数据
    */
    save(data) {
        this.innerSave(api.ZYGL_PARTITION_SAVE, data, (resp) => {
            var savedPartition = resp.object;
            this.setValueOjbect(savedPartition)
            this.setId(savedPartition.id);
            this.setType(ftypes.PARTITIONS);

            //添加到地图中
            mapManager.current.layers.partitionsSource.addFeature(this.getFeature());
            this.feature.setStyle(PartitionStyle.getStyle(resp.object));
        });
    }

    /**
     * 设置分区信息
     */
    setPartition(partition) {


    }
 
}

export default Partition;